package com.hhkj.payment.alipay.common.component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.hhkj.payment.alipay.common.conf.AliPayConf;
import com.ruoyi.common.exception.GlobalException;

/**
 * ClientProvider
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/15 15:21
 */
public class ClientProvider {

    /** default_Alipay_Client */
    private static DefaultAlipayClient defaultAlipayClient;

    /** 获取 default_Alipay_Client */
    public synchronized static DefaultAlipayClient getDefaultAlipayClient() {
        if (defaultAlipayClient == null) {
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(AliPayConf.getAppId());
            alipayConfig.setPrivateKey(AliPayConf.getPrivateKey());
            try {
                defaultAlipayClient = new DefaultAlipayClient(alipayConfig);
            } catch (AlipayApiException e) {
                throw new GlobalException("支付宝客户端创建失败，请联系管理员");
            }
        }
        return defaultAlipayClient;
    }
}
